package com.luobu.javaee.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    JwtTokenFilter jwtTokenFilter;

//    @Autowired
//    CustomAuthenticationProvider authenticationProvider;

//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.authenticationProvider(authenticationProvider);
//    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 配置JWT认证和授权规则
        http
                .authorizeRequests()
                .antMatchers("/user/login").permitAll() // 开放登录接口
                .antMatchers("/user/register").permitAll() // 开放注册接口
                .antMatchers("/user/email").permitAll() // 开放邮箱接口
       //         .anyRequest().authenticated() // 要求其他接口进行认证
                .and()
                .csrf().disable();

        // 添加JWT令牌过滤器
        http.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    public void configure(WebSecurity web) {
        // 配置放行的静态资源等
        web.ignoring().antMatchers("/static/**");
    }

//    @Override
//    @Bean
//    public AuthenticationManager authenticationManagerBean() throws Exception {
//        return super.authenticationManagerBean();
//    }

    // Other configurations...
}